package com.meilingcloud.biobank.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.ContainerType;
import com.meilingcloud.biobank.mapper.ContainerTypeMapper;
import com.meilingcloud.biobank.service.IContainerTypeService;
import com.meilingcloud.common.constant.Constants;
import com.meilingcloud.common.constant.UserConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 容器类型Service业务层处理
 *
 * @author chenl
 * @date 2021-03-03
 */
@Service
public class ContainerTypeServiceImpl extends ServiceImpl<ContainerTypeMapper, ContainerType> implements IContainerTypeService {

    @Override
    public List<ContainerType> queryList(ContainerType containerType) {
        LambdaQueryWrapper<ContainerType> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(containerType.getContainerTypeCode())) {
            lqw.eq(ContainerType::getContainerTypeCode, containerType.getContainerTypeCode());
        }
        if (StringUtils.isNotBlank(containerType.getContainerTypeName())) {
            lqw.like(ContainerType::getContainerTypeName, containerType.getContainerTypeName());
        }
        if (containerType.getContainerTypeLevel() != null) {
            lqw.eq(ContainerType::getContainerTypeLevel, containerType.getContainerTypeLevel());
        }
        return this.list(lqw);
    }

    @Override
    public String checkCodeUnique(ContainerType containerType) {
        Long containerTypeId = ObjectUtil.isNull(containerType.getContainerTypeId()) ? -1L : containerType.getContainerTypeId();
        ContainerType conType = baseMapper.selectOne(new LambdaQueryWrapper<ContainerType>()
                .eq(ContainerType::getContainerTypeCode, containerType.getContainerTypeCode()));
        if (ObjectUtil.isNotNull(conType) && !conType.getContainerTypeId().equals(containerTypeId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Cacheable(value = Constants.CONTAINER_TYPE_KEY, key = "#containerTypeId", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public ContainerType queryById(Long containerTypeId) {
        return this.getById(containerTypeId);
    }

    @CacheEvict(value = Constants.CONTAINER_TYPE_KEY, allEntries = true)
    @Override
    public boolean updateContainerById(ContainerType containerType) {
        return this.updateById(containerType);
    }

    @CacheEvict(value = Constants.CONTAINER_TYPE_KEY, allEntries = true)
    @Override
    public boolean removeContainerByIds(List<Long> containerIds) {
        return this.removeByIds(containerIds);
    }

    @Override
    public List<ContainerType> queryChildren(String containerTypeCode) {
        ContainerType containerType = this.getOne(new LambdaQueryWrapper<ContainerType>()
                .eq(ContainerType::getContainerTypeCode, containerTypeCode).last("limit 1"));
        List<ContainerType> list = this.list(new LambdaQueryWrapper<ContainerType>()
                .eq(ContainerType::getParentId, containerType.getContainerTypeId()));
        return list;
    }

    @Override
    public ContainerType queryByTypeCode(String containerTypeCode) {
        return this.getOne(new LambdaQueryWrapper<ContainerType>().eq(ContainerType::getContainerTypeCode, containerTypeCode).last("limit 1"));
    }
}
